﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OOAD_Project_Engine.Application.Services;
using OOAD_Project_Engine.Foundation.Interfaces;
using OOAD_Project_Engine.Domain.Entities;

namespace OOAD_Project_Editor.Domain.ManageSceneActions
{
    [Action(ActionType = EActionType.PasteEntity)]
    class PasteEntityAction : ManageSceneAction
    {
        private Entity pasteEntity;

        public PasteEntityAction(ISceneService scene)
            : base(scene)
        {
        }

        public override void DoAction()
        {
            Entity entity = EntityClipboard.GetInstance().GetEntity() as Entity;
            pasteEntity = new Entity(getSaveName(entity.Name));
            ICollection<IEntityComponent> entityComponents = entity;

            foreach (IEntityComponent c in entityComponents)
            {
                pasteEntity.Add(c.Clone() as IEntityComponent);
            }
            pasteEntity.Manager = this.Scene.Manager;
            this.Scene.Manager.Add(pasteEntity);
        }

        string getSaveName(string name)
        {
            string tempName = name;
            int count = 0;
            while (Scene.Manager.Find(tempName) != null)
            {
                tempName = name + "_" + count;
                count++;
            }
            return tempName;
        }

    }
}
